package com.yunyike.utils;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yunyike.constants.CommonConstant;
import com.yunyike.model.User;


public class UserUtils implements java.io.Serializable{
	
	private static final long serialVersionUID = 9076880758712320146L;
	
	private static Logger log = LoggerFactory.getLogger(UserUtils.class);
	
	public static String getSessionId(HttpServletRequest request,HttpServletResponse response){
		//检查cookie中是否含有sessionID的值
		String sessionId = "";
		sessionId = CookieUtils.getCookieValue(request, CommonConstant.USER_KEY_COOKIE);
		
		if(StringUtils.isBlank(sessionId)){
			sessionId = UUID.randomUUID().toString();
			CookieUtils.setCookie(response, CommonConstant.USER_KEY_COOKIE, sessionId, CommonConstant.USER_KEY_ET);
		}
		
		return sessionId;
	}
	
	/**
	 * 是否需要检查用户的位置
	 * @return
	 */
	public static boolean isCheckLocationByUser(User user,String openIds){
		
		if(StringUtils.isBlank(openIds)){
			log.info("配置文件中 没有配置openIds.");
			return true;
		}
		
		String userOpenId = user.getUserOpenId();
		log.info("当前配置文件：openIds："+openIds+",userOpenId:"+userOpenId);
		String[] split = openIds.split(",");
		for (String string : split) {
			if(StringUtils.isBlank(string)){
				continue;
			}
			
			if(string.equals(userOpenId)){
				log.info("内部人员不用校验用户的位置:userOpenId:{}",userOpenId);
				return false;
			}
		}
		
		return true;
	}
}
